<?php 

/**
 * @author Kartik
 * @params $_shipClass
 * @params Packing_Resource_Shippinglabel_[UPS/USPS/FEDEX]$_shipObject
 */
class Packing_Resource_Shippinglabel{

	const PERMISSION_DENIED = 1;
	/**
	 * @var string source_server_that_sends_slip_request
	 */
	protected $server;
	
	//Customer Details
	/**
	 * @var string User Id
	 */
	protected $userId;
	
	/**
	 * @var array Customer Details
	 * @params string firstName + lastName
	 * @params string address1 : (optional) correlates to address2 in database for USPS
	 * @params string address2
	 * @params string city
	 * @params string state
	 * @params integer zip5
	 * @params integer zip4: (optional)
	 */
	protected $customerDetails = array();
		
	//Order Details
	/**
	 * @var int/string $orderId
	 */
	protected $orderId;
	
	/**
	 * @var array Order Details
	 * @params date createdOn
	 * @params float total
	 * @params float adjustedTotal
	 * @params string payId
	 * 
	 */
	protected $orderDetails = array();
	
	/**
	 * @var array weight
	 * @params float pounds
	 * @params float ounces
	 */
	protected $Weight = array();
	
	
	//RMA Information
   	/**
   	 * @var array RMA
   	 * @params RMA
   	 * @params RMAPICFlag
   	 * @params RMABarcode
   	 */
   	protected $RMA = array();
	
	public function init(){
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
		$this->server = $bootstrap->getOption("server");
	}
   
	public function __construct(){
		$this->init();
	}
	
	public function setUserID(){
		if(Zend_Auth::getInstance()->getIdentity()){
			$this->user_id = Zend_Auth::getInstance()->getIdentity()->id;		
		}
	}
	
	public function getUserID(){
		return $this->user_id;
	}
	
	public function getCustomerDetails(){
		$user = Doctrine_Model_Users::getUser($this->getUserID());
		$address = Doctrine_Model_UserAddresses::getAddress($this->getUserID());
		$this->Customer = array(
			//Details needed for the USPS api
			"CustomerName"       =>    $user["firstName"] . " " . $user["lastName"],
			"CustomerAddress2"   =>    $address["address1"],
			"CustomerAddress1"   =>    $address["address2"], //It would appear the US Postal Service has a sense of humor
			"CustomerCity"       =>    $address["city"],
			"CustomerState"      =>    $address["state"],
			"CustomerZip5"       =>    $address["zip"],
			"CustomerZip4"       =>    "",
			//Other additional details required for the final PDF created by the website
		);
		
		return $this->Customer;
	}
	
	public function setOrderID($order_id){
		if(Doctrine_Model_Orders::userHasOrder($order_id)){
			$this->order_id = $order_id;
			$this->order_details = $this->getOrderDetails();
		}else{
			throw new Exception(self::PERMISSION_DENIED);
		}
		
	}
	
	public function getOrderID(){
		return $this->order_id;
	}
	
	public function getOrderDetails(){
		$order = Doctrine_Model_Orders::getOrderDetails($this->getOrderID());
		$orderItems = Doctrine_Model_OrderItems::getOrderItems($this->getOrderID());
		
		$this->order_details = array(
			"OrderId" => $order["id"],
			"OrderDate" => $order["createdOn"],
			"PaymentType" => $order["payId"],
			"Total" => $order["total"],
			"AdjustedTotal" => $order["adjustedTotal"],         
			//Order Details - Later
		);
		$this->order_details = array_merge($this->order_details, array("Count" => count($orderItems)));

		return $this->order_details;
	}
	
	public function setOrderWeight($pounds = 0, $ounces = 0){
		if($pounds){
			$this->Weight["Pounds"] = $pounds;
			if($ounces)
				$this->Weight["Ounces"] = $ounces;
			else
				$this->Weight["Ounces"] = $pounds*16;
		}else{
			$weight = 0;
			$orderItems = Doctrine_Model_OrderItems::getOrderItems($this->getOrderID());
			foreach($orderItems as $items){
				$weight += $items['weight']; 
			}
			$this->Weight["Pounds"] = ceil($weight/100);
			$weight *= 16;
			$this->Weight["Ounces"] = ceil($weight/100);
		}
	}
	
	public function getOrderWeight(){	
		return $this->Weight;	
	}
	
	public function setRMA($part = 0){
		$this->RMA["RMA"] = $this->server["acronym"] . "-" . $this->order_id;
		if($part) //For Multipart Labels
			$this->RMA["RMA"] .= "-" . $part;
	}
	
	public function getRMA(){
		return $this->RMA["RMA"];
	}
	
	public function setWeightInPounds($weightInPounds){
      $this->Weight["Pounds"] = $weightInPounds;
   }

   public function setWeightInOunces($weightInOunces){
      $this->Weight["Ounces"] = $weightInOunces;
   }

   public function getWeightInPounds(){
      return $this->Weight["Pounds"];
   }

   public function getWeightInOunces(){
      return $this->Weight["Ounces"];
   }

   public function adjustWeightInPoundsBy($weight){
      $this->Weight["Pounds"] -= $weight;
   }

   public function adjustWeightInOuncesBy($weight){
      $this->Weight["Ounces"] -= $weight;
   }

   public function setCustomerDetails($customerDetails){
      $this->Customer = $customerDetails;
   }

   public function displayCustomerDetails($customerDetails){
      return $this->Customer;
   }
}